// pages/register/index.js
import {
  loginMpWxMobile
} from "../../apis/index"
Page({
  data: {
    partyData: {},
    mobile: "",
  },
  //处理获取手机号码的结果
  async handleLogin() {
    const app = this
    console.log(this.data.partyData)
    console.log("mobile", this.data.mobile)
    const code = await this.getCode()
    console.log('code==', code)
    const partyData = this.data.partyData
    let form = {
      form: {
        code: code,
        isParty: false,
        mobile: this.data.mobile,
        partyData: partyData
      }
    }
    console.log(form.form)
    const res = await loginMpWxMobile({
      data: form
    })
    console.log("注册结果", res)
    if (res.status == 200) {
      wx.setStorageSync('token', res.data.token)
      wx.setStorageSync('avatarUrl', partyData.userInfo.avatarUrl)
      wx.setStorageSync('nickName', partyData.userInfo.nickName)
      let url = "/pages/user/index"
      wx.switchTab({
        url: url,
      })
    }
  },
  getCode() {
    return new Promise((resolve, reject) => {
      wx.login({
        success(res) {
          resolve(res.code)
        }
      })
    })
  },
  onLoad(options) {
    console.log('上一级传递过来的数据', options.partyData)
    //将传递过来的参数转化成JSOn对象
    const partyData = JSON.parse(decodeURIComponent(options.partyData))
    this.setData({
      partyData: partyData
    })
  },
})